﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ClassFolio.Model
{
    public class Sequence
    {
        // Public
        public void Open()
        {
            SqlGate.Open(this);
        }
        public void Next()
        {
            SqlGate.Next(this);
        }
        public void Prior()
        {
            SqlGate.Prior(this);
        }

        /// <summary>
        /// Названия столбцов в выборке
        /// </summary>
        public List<string> Caption = new List<string>();
        
		/// <summary>
        /// Результат выборки
        /// </summary>
        public List<List<string>> Result = new List<List<string>>();

        public string Sql { get; set; }
        public SqlSwitch SqlSwitch { get { return this._sqlSwitch; } }
        public int Index { get; set; }
        public int RecordCount { get; set; }

        public static ISqlGate SqlGate { get; set; }

        private SqlSwitch _sqlSwitch = new SqlSwitch();

    }
        
    public class SqlSwitch
    {
        public void Add(string name, string value, SqlCondition condition, SqlType type, Direction dir)
        {
            SqlCase sqlCase = this._condition.Find(i => i.Name == name);
            if (sqlCase != null)
                this._condition.Remove(sqlCase);

            this._condition.Add(new SqlCase(name, value, condition, type, dir));

        }
        public void Clear()
        {
            this._condition.Clear();
        }
        public List<SqlCase> Condition 
        {
            get { return _condition; }
            set { this._condition = value; } 
        }

        private List<SqlCase> _condition = new List<SqlCase>();
    }

	[Serializable]
    public class SqlCase
    { 
        // Constructors
        public SqlCase(string name, string values, SqlCondition condition, SqlType type, Direction dir)
        {
            this._name = name;
            this._values = values;
            this._sqlCondition = condition;
            this._sqlType = type;
            this._dir = dir;
        }

        // Properties
        public string Name { get { return this._name; } }
        public string Values { get { return this._values; } }
        public SqlCondition SqlCondition { get { return this._sqlCondition; } }
        public SqlType SqlType { get { return this._sqlType; } }
        public Direction Dir { get { return this._dir; } }

        //Private
        private string _name;
        private string _values;
        private SqlCondition _sqlCondition;
        private SqlType _sqlType;
        private Direction _dir;
    }

	[Serializable]
    public enum SqlCondition { 
        equal,      /// равно
        not_eqaul,  /// не равно
        gt,         /// больше 
        lt,         /// меньше 
        gte,        /// больше или равно
        lte,        /// меньше или равно
        btw,        /// между
        like,       /// похоже
        in_enum     /// вхождение в последовательность
    }

	[Serializable]
    public enum SqlType
    { 
        sqlInt,
        sqlString,
        sqlDate,
        sqlDouble
    }

	[Serializable]
    public enum Direction { input, output }

    public interface ISqlGate
    {
        void Open(Sequence sequence);
        void Next(Sequence sequence);
        void Prior(Sequence sequence);
    }

}
